如何从adb shell启动和停止android服务?

您所在的位置:网站首页 安卓 服务 如何从adb shell启动和停止android服务?

如何从adb shell启动和停止android服务?

2024-04-13 14:13| 来源: 网络整理| 查看: 265

我需要编写一个Shell脚本来启动和停止android服务。

我是Android的初学者,但是可以这样工作:

在AndroidManifest.xml中,确保您在中具有以下内容:

12345                

其中YourServiceSubClassName扩展android.app.Service是作为服务的Java类。其中com.some.package是程序包名称,对我来说,在AndroidManifest.xml和Java中都是如此。 使用javabeat.net文章作为帮助,查找

还要注意,在文本中包名称和类名称之间应该有.service.,我想这是一些约定,但是对我来说,这导致了我尚未解决的ClassNotFoundException。

然后,安装您的apk。我是从eclipse做的,但是adb install -r yourApkHere.apk应该也可以。卸载是adb uninstall com.some.package.name,顺便说一句。

您可以像这样从主机系统启动它,谢谢Tim和MrRoy:

1adb shell am startservice com.some.package.name/.YourServiceSubClassName

有趣的是,我不需要-n。

要停止,我用

1adb shell am force-stop com.some.package.name

希望能帮助到你。

由于我是初学者,请随时进行编辑/注释以纠正任何误解(例如,可能与组件(?)名称中的.service.有关)。

相关讨论 所以,这就是为什么我会收到此错误? $ am startservice com.adguard.android/.vpn.AdguardVpnService启动服务:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.adguard.android / .vpn.AdguardVpnService 错误:需要权限android.permission.BIND_VPN_SERVICE

启动服务:

1adb shell am startservice ...

start a Service. Options are: --user | current: Specify which user to run as; if not specified then run as the current user.

停止服务:

1adb shell am stopservice ...

stop a Service. Options are: --user | current: Specify which user to run as; if not specified then run as the current user.

如果要在adb shell中运行脚本,那么我尝试使用应用程序来执行相同的操作。我认为您可以使用" am start"命令

用法:am [子命令] [选项]

123456789101112131415161718192021222324252627282930313233343536373839start an Activity: am start [-D] [-W]     -D: enable debugging     -W: wait for launch to complete **start a Service: am startservice ** send a broadcast Intent: am broadcast start an Instrumentation: am instrument [flags]     -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)     -e : set argument to     -p : write profiling data to     -w: wait for instrumentation to finish before returning start profiling: am profile start stop profiling: am profile stop start monitoring: am monitor [--gdb ]     --gdb: start gdbserv on the given port at crash/ANR specifications include these flags:     [-a ] [-d ] [-t ]     [-c [-c ] ...]     [-e|--es ...]     [--esn ...]     [--ez ...]     [-e|--ei ...]     [-n ] [-f ]     [--grant-read-uri-permission] [--grant-write-uri-permission]     [--debug-log-resolution]     [--activity-brought-to-front] [--activity-clear-top]     [--activity-clear-when-task-reset] [--activity-exclude-from-recents]     [--activity-launched-from-history] [--activity-multiple-task]     [--activity-no-animation] [--activity-no-history]     [--activity-no-user-action] [--activity-previous-is-top]     [--activity-reorder-to-front] [--activity-reset-task-if-needed]     [--activity-single-top]     [--receiver-registered-only] [--receiver-replace-pending]     [] 相关讨论 该帮助页面确实无法帮助您弄清楚如何指定意图。 特别是的语法是神秘的。 @pzulw只需将Tims的答案与MrRoys结合,然后根据此链接上的将内容放入AndroidManifest.xml中,就可以了。

我可以通过开始服务

1am startservice com.xxx/.service.XXXService

但是我还不知道如何阻止它。

相关讨论 您可以使用广播接收器停止服务,然后将广播发送到该接收器。

使用时可能会出现错误" *错误:应用程序在后台*"

1adb shell am startservice

在奥利奥(26岁以上)。这需要前台的服务。 使用以下内容。

1adb shell am start-foreground-service com.some.package.name/.YourServiceSubClassName

您应将服务的android:exported属性设置为" true",以便允许其他组件调用它。在AndroidManifest.xml文件中,添加以下属性:

1

然后,您应该能够通过adb启动该服务:

1adb shell am startservice com.package.name/.YourServiceName

有关android:exported属性的更多信息,请参见此页面。

您需要添加android:exported="true"才能从ADB命令行启动服务。然后您的清单看起来像这样:

1234567891011                                

然后从亚行

要开始服务:

adb shell am startservice com.your.package.name/.YourServiceName

停止服务(在棉花糖上):

adb shell am stopservice com.your.package.name/.YourServiceName

要停止服务(在Jelly Bean上):

adb shell am force-stop com.your.package.name

响应pzulw对sandroid的有关指定意图的反馈。

组件名称的格式在ComponentName.unflattenFromString的api文档中进行了描述

It splits the string at the first '/', taking the part before as the package name and the part after as the class name. As a special convenience (to use, for example, when parsing component names on the command line), if the '/' is immediately followed by a '.' then the final class name will be the concatenation of the package name with the string following the '/'. Thus"com.foo/.Blah" becomes package="com.foo" class="com.foo.Blah".

am startservice

或实际上是从OS Shell

adb shell am startservice

对于仍然对如何定义服务名称参数感到困惑的任何人,正斜杠会在完全限定的类名称中紧随应用程序包名称之后。

因此,给定应用程序包名称为:app.package.name

以及以下服务的完整路径:app.package.name.example.package.path.MyServiceClass

然后,该命令将如下所示:

1adb shell am startservice app.package.name/.example.package.path.MyServiceClass



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3